﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseUI;
using Utility;
using Hxj.Data;
using System.Data;
using Utility.Model;

namespace MsgCenter.Web.Pages.Module
{
    public partial class Option : BasePage
    {
        protected string strType = ParamsOfRequest.Request("type");
        protected string strDeptGuID = ParamsOfRequest.Request("DeptGuID");
        public string OrderByListItems_UniqueID;
        protected void Page_Load(object sender, EventArgs e)
        {
            OrderByListItems_UniqueID = this.lstBox.UniqueID.Replace("$", "_");
            if (!this.IsPostBack)
            {
                if (strType == "Add")
                {
                    pnlOption.Visible = true;
                }
                else if (strType == "Edit")
                {
                    Sys_Module model = BLL.BLLServiceHelper.GetModuleService().GetModel(strDeptGuID);
                    if (model != null)
                    {
                        txtDeptName.Value = model.ModuleName;
                        txtCssCss.Value = model.CssCss;
                        txtLinkAddress.Value = model.LinkAddress;
                        txtDes.Text = model.ModuleDes;
                    }
                    pnlOption.Visible = true;
                }
                else if (strType == "Sort")
                {
                    Sys_Module model = BLL.BLLServiceHelper.GetModuleService().GetModel(strDeptGuID);
                    WhereClip wcp = new WhereClip();
                    if (model != null)
                    {
                        wcp = wcp.And(Sys_Module._.FatherID == model.ModuleID);
                    }
                    else
                    {
                        wcp = wcp.And(Sys_Module._.FatherID == "0");
                    }
                    using (DataTable dt = BLL.BLLServiceHelper.GetModuleService().GetModuleTable(wcp))
                    {
                        DataRow[] drs = dt.Select("", "Sort");
                        foreach (DataRow dr in drs)
                        {
                            this.lstBox.Items.Add(new ListItem(dr["ModuleName"].ToString(), dr["ID"].ToString()));
                        }
                    }
                    pnlSort.Visible = true;
                }
            }
        }

        protected void linkSave_Click(object sender, EventArgs e)
        {
            if (strType == "Sort")
            {
                try
                {
                    string[] sz = hidValue.Value.Split(',');
                    if (sz.Length > 0)
                    {
                        BLL.BLLServiceHelper.GetModuleService().Update_Sort(sz);
                        base.ShowAlertScript("Close_Reload();");
                    }
                }
                catch (Exception ex)
                {
                    base.ShowAlert("保存数据出错:" + ex.Message);
                }
            }
            else if (strType == "Edit")
            {
                try
                {
                    Sys_Module model = BLL.BLLServiceHelper.GetModuleService().GetModel(strDeptGuID);
                    if (model != null)
                    {
                        model.ModuleName = txtDeptName.Value;
                        model.ModuleName = txtDeptName.Value;
                        model.CssCss = txtCssCss.Value;
                        model.LinkAddress = txtLinkAddress.Value;
                        model.ModuleDes = txtDes.Text;
                        BLL.BLLServiceHelper.GetModuleService().Save(model);
                        base.ShowAlertScript("Close_Reload();");
                    }
                    else
                    {
                        throw new Exception("获取修改对象实体为空");
                    }
                }
                catch (Exception ex)
                {
                    base.ShowAlert("保存数据出错:" + ex.Message);
                }
            }
            else if (strType == "Add")
            {
                try
                {
                    Sys_Module model = new Sys_Module();
                    model.ModuleName = txtDeptName.Value;
                    model.CssCss = txtCssCss.Value;
                    model.LinkAddress = txtLinkAddress.Value;
                    model.ModuleDes = txtDes.Text;
                    if (strDeptGuID == string.Empty || strDeptGuID == Guid.Empty.ToString())
                    {
                        strDeptGuID = "0";
                    }
                    model.FatherID = strDeptGuID;
                    model.CreateUser = base.Account;
                    BLL.BLLServiceHelper.GetModuleService().Save(model);
                    base.ShowAlertScript("Close_Reload();");
                }
                catch (Exception ex)
                {
                    base.ShowAlert("保存数据出错:" + ex.Message);
                }
            }
        }
    }
}